var api = {
  path: 'https://siniso.fstuis.com',
  url: 'https://siniso.fstuis.com/api/v1/',//正式
  //url: 'https://tsiniso.fstuis.com/api/v1/',//测试
  color: '#383838',
  obj: wx,
  cache(key, data) {

    if (data == undefined) {

      return this.obj.getStorageSync(key) || ''
    }

    this.obj.setStorageSync(key, data)
  },
  tip: function (title) {

    wx.showToast({
      icon: 'none',
      title: title
    })

  },
  upLoadImage: (imageList) => {
    wx.showLoading({
      title: '加载中',
    })
    let num = 0;
    let fileUpload = new Array();
    return new Promise(function (resolve, reject) {
      for (let i = 0; i < imageList.length; i++) {
        wx.uploadFile({
          url: api.path + '?name=img',
          filePath: imageList[i],
          name: 'img',
          success: function (res) {
            let data = res.data;
            data = JSON.parse(res.data);
            fileUpload.push(data.img);
            if (num == imageList.length - 1) {
              resolve(fileUpload);
            }
            num += 1;
          }
        })
      }
      wx.hideLoading()
    });
  },
  is_tel: function (tel) {

    return /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(tel)

  },
  get_configs: function (key) {

    return this.get('get_configs', {
      key: key || ''
    })
  },
  alert: function (content, title) {

    var _this = this

    return new Promise((resove, reject) => {

      wx.showModal({
        title: title || '',
        content: content,
        showCancel: false,
        confirmColor: _this.color,
        cancelColor: '#999',
        success(r) {
          if (r.confirm) {

            resove(r)

          }
        }
      })

    })

  },
  confirm: function (url, data, title, content) {

    var _this = this

    if (!title) {

      title = '删除提示'
    }

    if (!content) {

      content = '你确定要删除吗?'
    }

    return new Promise((resove, reject) => {

      wx.showModal({
        title: title,
        content: content,
        confirmColor: _this.color,
        cancelColor: '#FE8A01',
        success(r) {
          if (r.confirm) {

            resove(_this.post(url, data))

          }
        }

      })

    })

  },
  get: function (url, data) {

    return this.request(url, 'GET', data)

  },
  request: function (url, method, data) {

    //wx.showNavigationBarLoading()

    var _this = this

    var token = wx.getStorageSync('token')

    var method = method || 'POST'

    return new Promise((resove, reject) => {

      wx.request({
        url: _this.url + url,
        data: data || {},
        header: {
          token: token
        },
        method: method,
        success: res => {

          if (res.statusCode != 200) { //服务器错误

            reject(res)

            if (method == 'POST') {

              _this.alert(res.data)

            }

            return
          }
          if(res.data.code !=200){
            reject(res)
          }
          resove(res.data)

        },
        fail: res => {

          reject(res)
        },
        complete: res => {
          wx.stopPullDownRefresh()

          wx.hideNavigationBarLoading()

          wx.hideLoading()
        }
      })

    })
  },
  post: function (url, data) {

    wx.showLoading({
      mask: true
    })

    return this.request(url, 'POST', data)

  },
  update_user_info: function (name, img) {

    return this.post('users', {
      "input": {
        "nick_name": name,
        "img": img
      }
    })
  },
  async wxMiniAppLogin() {
    await new Promise((resolve,reject)=>{
      wx.login({
        success: res => {
          var url = 'login'
          var data = {
            code: res.code
          }
          this.request(url, 'POST', data).then(res => {
            if (res.code == 200) {
              wx.setStorageSync('user', res.data)
              resolve(res.data)
            }
          })
        }
      })
    })
  },
  update_view: function (id, table) {

    this.request('update_view', 'POST', {
      id: id || 0,
      table: table || 0
    })

  },
  get_total: function () {

    return this.get('get_total')
  },
  upload: function () {

    var _this = this

    return new Promise((resove, reject) => {

      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: res => {
          console.log(res)
          var tempFiles = res.tempFiles

          var tempFilePaths = res.tempFilePaths

          wx.showLoading({
            mask: true
          })

          wx.uploadFile({
            url: _this.url + 'upload',
            filePath: tempFilePaths[0],
            name: 'file',
            success: res => {

              const data = JSON.parse(res.data)

              if (data.code == 200) {

                resove(data)

              }

            },
            fail: res => {

              reject(res)
            },
            complete: res => {

              wx.hideLoading()
            }
          })
        }
      })

    })
  },
  checkLogin: function () {
    let userInfo = api.cache('user')
    if (!userInfo.nickName && !userInfo.headimgurl) {
      wx.navigateTo({
        url: '/pages/grant/grant',
      })
      return false;
    }
    return true;
  },
  saveFoot:function(id,type){
    let params = {
      id:id,
      data_type:type,
      member_id:this.cache("user").member_id
    }
    this.post('click_history',params).then(res => {})
  },
  distance: function (la1, lo1, la2, lo2) {
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137; //地球半径
    s = Math.round(Math.round(s * 10000) / 10000);
    console.log("计算结果", s);
    return s;
  }
};

module.exports = api